/*
 * Copyright (c) 2007-2020 西安交通信息投资营运有限公司 版权所有
 */

package com.xci.edu.core.service;

import com.github.lvyanyang.annotation.OperateLog;
import com.xci.edu.core.entity.ExamQuestion;
import com.xci.edu.core.entity.ExamRecordDetails;
import com.xci.edu.core.dao.ExamRecordDetailsDao;
import com.xci.edu.core.filter.ExamRecordDetailsFilter;
import com.github.lvyanyang.core.BaseService;
import com.github.lvyanyang.core.BoolResult;
import com.github.lvyanyang.core.PageList;
import com.github.lvyanyang.core.Param;
import com.github.lvyanyang.core.XCI;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.Cache;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 考试试题明细服务
 * @author 马星俊 maxingjun@xci96716.com
 * @since 2020-11-25 10:52:08
 */
@Service
public class ExamRecordDetailsService extends BaseService {
    /** 考试试题明细数据层对象 */
    @Resource private ExamRecordDetailsDao examRecordDetailsDao;

    /**
     * 新建考试试题明细
     * @param entity 考试试题明细实体
     */
    @OperateLog(msg = "新建考试试题明细", module = "考试试题明细")
    @Transactional(rollbackFor = Exception.class)
    public BoolResult insert(ExamRecordDetails entity) {
        return save(entity, true);
    }

    /**
     * 修改考试试题明细
     * @param entity 考试试题明细实体
     */
    @OperateLog(msg = "修改考试试题明细", module = "考试试题明细")
    @Transactional(rollbackFor = Exception.class)
    public BoolResult update(ExamRecordDetails entity) {
        return save(entity, false);
    }

    /**
     * 修改考试试题明细状态
     * @param ids    考试试题明细主键字符串
     * @param status 考试试题明细状态
     */
    @OperateLog(msg = "修改考试试题明细状态", module = "考试试题明细")
    @Transactional(rollbackFor = Exception.class)
    public void updateStatus(@NotBlank(message = "请指定考试试题明细主键字符串") String ids, @NotNull(message = "请指定考试试题明细状态") Boolean status) {
        XCI.forEachIds(ids, id -> {
            examRecordDetailsDao.updateStatus(id, status);
        });
    }

    /**
     * 根据主键查询单个考试试题明细
     * @param id 考试试题明细主键
     * @return 返回考试试题明细实体
     */
    public ExamRecordDetails selectById(@NotNull(message = "请指定考试试题明细主键") Long id) {
        return examRecordDetailsDao.selectById(id);
    }

    /**
     * 删除考试试题明细
     * @param ids 考试试题明细主键字符串
     */
//    @OperateLog(msg = "删除考试试题明细", module = "考试试题明细")
//    @Transactional(rollbackFor = Exception.class)
//    public BoolResult delete(@NotBlank(message = "请指定考试试题明细主键字符串") String ids) {
//        XCI.forEachIds(ids, id -> {
//            var entity = examRecordDetailsDao.selectById(id);
//            if (entity == null) return;
//
//            examRecordDetailsDao.deleteById(entity.get());
//            //logService.saveDeleteHistoryLogAsync(entity.get(), entity);
//        });
//        return BoolResult.success();
//    }


    /**
     * 根据主键查询考试试题列表
     * @param recordId 考试记录主键
     * @return 返回考试试题列表
     */
    public List<ExamQuestion> selectQuestionByRecordId(@NotNull(message = "请指定考试试题明细主键") Long recordId) {
        return examRecordDetailsDao.selectQuestionByRecordId(recordId);
    }

    /**
     * 根据主键查询分数
     * @param recordId 考试记录主键
     * @return 学员分数
     */
    public Integer selectScoreByRecordId(@NotNull(message = "请指定考试试题明细主键") Long recordId) {
        return examRecordDetailsDao.selectScoreByRecordId(recordId);
    }

    /**
     * 查询考试试题明细列表
     * @param filter 过滤条件
     * @return 返回考试试题明细列表
     */
    public List<ExamRecordDetails> selectList(ExamRecordDetailsFilter filter) {
        return examRecordDetailsDao.selectList(filter);
    }

    /**
     * 查询考试试题明细分页列表
     * @param filter 过滤条件
     * @return 返回考试试题明细分页列表
     */
    public PageList<ExamRecordDetails> selectPageList(ExamRecordDetailsFilter filter) {
        return PageList.of(examRecordDetailsDao.selectPageList(filter));
    }

    /**
     * 保存数据,在insert和update之前和之后的校验和处理
     * @param entity  考试试题明细实体
     * @param created 是否新建
     */
    private BoolResult save(ExamRecordDetails entity, boolean created) {

        //更新数据库
        if (created) {
            examRecordDetailsDao.insert(entity);
            //logService.saveInsertHistoryLogAsync(entity.get(), entity);
        } else {
            //var before = examRecordDetailsDao.selectById(entity.get());
            examRecordDetailsDao.update(entity);
            //logService.saveUpdateHistoryLogAsync(entity.get(), before, entity);
        }
        return BoolResult.success();
    }
}